package Test;

/**
 * 旋转数组的中间值
 * @leiwenkang 2018-01-05 12:36.
 */
public class FindMidNum {
    public static void main(String[] args) {
         String line = "1,2,3,4,5,6,7";
         String result = solution(line);
        System.out.println(result);
    }

    private static String solution(String line) {
        if (line == null || line.length() == 0) {
            return null;
        }
        String[] str = line.split(",");
        int[] array = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            array[i] = Integer.parseInt(str[i]);
        }
        int start = 0;
        int end = array.length - 1;
        int min;
        if (array[start] < array[end]){
            return array[array.length/2] + "";
        } else if (array[start] == array[end] && array[start] == array[array.length/2]) {
            min = array[0];
            for(int i=1;i<array.length-1;i++){
                if(array[i]<min){
                    min = array[i];
                }
            }
        } else {
            while (start !=  end - 1) {
                int mid = (start + end) / 2;
                if (array[mid] >= array[start]){
                    start = mid;
                } else {
                    end = mid;
                }
            }
            min = end;
        }
        int index = -1;

        for (int i = min; i < array.length; i++) {
            index++;
            if (index == array.length/2) {
                return array[i] + "";
            }
        }
        int result = 0;
        index++;
        while (index != array.length/2) {
            index++;
            result++;
        }
        return array[result] + "";
    }

    public static int orderFindMin(int[] array){
        int min = array[0];
        for(int i=1;i<array.length-1;i++){
            if(array[i]<min){
                min = array[i];
            }
        }
        return min;
    }
}
